package com.iaiai.imail.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.iaiai.imail.core.IActivity;
import com.iaiai.imail.core.IServiceFactory;
import com.iaiai.imail.entity.Email;
import com.iaiai.imail.helper.MessageBox;
import com.iaiai.imail.helper.mail.MultiMailsender;
import com.iaiai.imail.util.DateUtil;

/**
 * 
 * <p>
 * Title: CreateTestActivity.java
 * </p>
 * <p>
 * E-Mail: 176291935@qq.com
 * </p>
 * <p>
 * QQ: 176291935
 * </p>
 * <p>
 * Http: iaiai.iteye.com
 * </p>
 * <p>
 * Create time: 2012-9-13 下午2:18:05
 * </p>
 * 
 * @author 丸子
 * @version 0.0.1
 */
public class CreateTestActivity extends IActivity {

	private TextView email;
	private TextView type; // 类型
	private TextView getServiceText; // 接收服务器
	private TextView getPort; // 接收服务器端口
	private TextView sendServiceText; // 接收服务器
	private TextView sendPort; // 接收服务器端口

	private int get_port = 110; // 默认接收端口
	private int send_port = 25; // 默认发送端口
	private String send_type = "smtp";
	private String get_type = "pop3";
	private String service;	//服务器
	private String sendService;	//发送服务器

	private boolean finish = false; // 邮件发送状态

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.create_test);

		setTitleLayout(); // 设置头

		setTitleText(getResources().getString(R.string.create_email_title));

		email = (TextView) findViewById(R.id.email);
		email.setText(getResources().getString(R.string.account1) + getBundle().getString("email"));
		type = (TextView) findViewById(R.id.type);
		type.setText(getResources().getString(R.string.type) + getBundle().getString("type"));
		getServiceText = (TextView) findViewById(R.id.get_service);
		getServiceText.setText(getResources().getString(R.string.get_service) + get_type + "."
				+ getBundle().getString("email").substring(getBundle().getString("email").indexOf("@") + 1));
		service = get_type + "." + getBundle().getString("email").substring(getBundle().getString("email").indexOf("@") + 1);
		getPort = (TextView) findViewById(R.id.get_port);
		getPort.setText(getResources().getString(R.string.get_port) + get_port);
		sendServiceText = (TextView) findViewById(R.id.send_service);
		sendServiceText.setText(getResources().getString(R.string.get_service) + send_type + "."
				+ getBundle().getString("email").substring(getBundle().getString("email").indexOf("@") + 1));
		sendService = send_type + "." + getBundle().getString("email").substring(getBundle().getString("email").indexOf("@") + 1);
		sendPort = (TextView) findViewById(R.id.send_port);
		sendPort.setText(getResources().getString(R.string.get_port) + send_port);

		// //////////////上一步
		findViewById(R.id.pre).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent activity = new Intent(CreateTestActivity.this, CreateInfoActivity.class);
				activity.putExtras(getAttribute());
				startActivity(activity);
				finish(); // 关闭当前
			}
		});
		// ///////////////测试
		findViewById(R.id.test).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.show();
				execHandle(new Handler() {
					@Override
					public void query() {
						testEmail();
					}
				}, new Runnable() {
					@Override
					public void run() {
						dialog.dismiss();
						if (!finish) {
							MessageBox.showToast(CreateTestActivity.this, R.string.msg_create_send_test_error);
						} else {
							MessageBox.showToast(CreateTestActivity.this, R.string.msg_create_send_test);
						}
					}
				});
			}
		});
		// //////////////完成
		findViewById(R.id.finish).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.show();
				execHandle(new Handler() {
					@Override
					public void query() {
						createEmail(); // 创建帐号
					}
				}, new Runnable() {
					@Override
					public void run() {
						dialog.dismiss();

						MessageBox.showToast(CreateTestActivity.this,
								getResources().getString(R.string.msg_create_success));
						Intent activity = new Intent(CreateTestActivity.this, MainActivity.class);
						startActivity(activity);
						finish();
					}
				});
			}
		});
	}

	/**
	 * 存储数据
	 * 
	 * @return
	 */
	private Bundle getAttribute() {
		Bundle bundle = getBundle();
		return bundle;
	}

	/**
	 * 测试邮件
	 */
	private void testEmail() {
		String sendEmail = send_type + "."
				+ getBundle().getString("email").substring(getBundle().getString("email").indexOf("@") + 1);
		String username = getBundle().getString("email").substring(0, getBundle().getString("email").indexOf("@"));
		String password = getBundle().getString("password");
		String email = getBundle().getString("email");

		// 这个类主要是设置邮件
		MultiMailsender.MultiMailSenderInfo mailInfo = new MultiMailsender.MultiMailSenderInfo();
		mailInfo.setMailServerHost(sendEmail);
		mailInfo.setMailServerPort(String.valueOf(send_port));
		mailInfo.setValidate(true);
		mailInfo.setUserName(username);
		mailInfo.setPassword(password);// 您的邮箱密码
		mailInfo.setFromAddress(email);
		mailInfo.setToAddress(email);
		mailInfo.setSubject(getResources().getString(R.string.msg_create_test));
		mailInfo.setContent(getResources().getString(R.string.msg_create_test));
		String[] receivers = new String[] { email, email };
		String[] ccs = receivers;
		// 这个类主要来发送邮件
		MultiMailsender sms = new MultiMailsender();
		finish = sms.sendTextMail(mailInfo);
	}

	/**
	 * 创建email
	 */
	private void createEmail() {
		String sendEmail = send_type + "."
				+ getBundle().getString("email").substring(getBundle().getString("email").indexOf("@") + 1);
		String username = getBundle().getString("email").substring(0, getBundle().getString("email").indexOf("@"));
		String password = getBundle().getString("password");
		String account = getBundle().getString("email");

		// 判断是否有此帐号的邮件
		Email email = IServiceFactory.getInstance().getEmailDao().queryEmail(account);
		if (email != null) {
			MessageBox.showToast(this, getResources().getString(R.string.msg_create_repeat));
			return;
		}

		email = new Email();
		email.setAccount(account);
		email.setUserName(username);
		email.setPassword(password);
		email.setCreateDate(DateUtil.getDateTime());
		email.setServicePort(get_port);
		email.setSendPort(send_port);
		email.setServiceAddress(service);
		email.setSendService(sendService);

		IServiceFactory.getInstance().getEmailDao().createEmail(email);
	}

	/**
	 * 设置头
	 */
	private void setTitleLayout() {
		if (getBundle().getBoolean("create")) {
			// 显示返回按钮
			insertTitleLayoutButton(R.drawable.icon_return, new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent activity = new Intent(CreateTestActivity.this, MainActivity.class);
					startActivity(activity);
					finish();
					overridePendingTransition(R.anim.push_down_out, R.anim.push_down_in);
				}
			});
		}
	}

}
